package com.cicadasmall.common.utils;

import com.cicadasmall.common.constant.Constant;
import com.cicadasmall.common.func.Fn;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * EvnUtils
 *
 * @author Jin
 */
public class EvnUtils {

    public static boolean isJarActive(Class<?> clazz) {
        URL url = clazz.getResource("");
        String protocol = url.getProtocol();
        return Fn.equal("jar", protocol);
    }

    public static String checkAndCreateResourceDir(String dir) {
        String path = Constant.RESOURCES_FOLDER_NAME + File.separator + dir;
        File folder = new File(path);
        if (!folder.exists()) folder.mkdirs();
        return folder.getAbsolutePath();
    }

    public static String checkAndCreateNewFile(String dir) throws IOException {
        String path = Constant.RESOURCES_FOLDER_NAME + File.separator + dir;
        File file = new File(path);
        if (file.isDirectory()) file.delete();
        if (!file.exists()) file.createNewFile();
        return file.getAbsolutePath();
    }

}
